package com.xzz.mybatis.generator.utils;

import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * your Description

 */
@UtilityClass
public class MyStringUtil {

    /**
     * 固定位数填充指定字符串, e.g.填充6位为0  15 -> 000015
     * @param originStr 待填充字符串
     * @param width
     * @param fchar
     * @return
     */
    public static String fillCharInFixedWith(String originStr,int width, String fchar){
        originStr = originStr==null ? "" : originStr;
        originStr = originStr.trim();
        int delta = width - originStr.length();
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i< delta; i++ ){
            stringBuilder.append(fchar);
        }
        stringBuilder.append(originStr);
        return stringBuilder.toString();
    }


    /**
     * 生成 2020081201_1 ，2020081201_2 单号
     * @param targetPurchaseNo 单号
     * @param comm 标识符
     * @return String
     */
    public static String fillNewPurchaseNo(String targetPurchaseNo,String comm) {
        StringBuilder stringBuilder = new StringBuilder();
        if (targetPurchaseNo.contains(comm)) {
            String[] purchaseArr = targetPurchaseNo.split(comm);
            String suffix = purchaseArr[1];
            stringBuilder.append(purchaseArr[0]).append(comm).append(Integer.valueOf(suffix) + 1);
            return stringBuilder.toString();
        }
        return stringBuilder.append(targetPurchaseNo).append(comm).append(1).toString();
    }

    /**
     * 截取堆栈异常
     * @param e
     * @return
     */
    public static String stackTraceToString(Exception e){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        int maxStrLength = sw.toString().length();
        maxStrLength = maxStrLength > 5000 ? 5000 : maxStrLength;
        return sw.toString().substring(0, maxStrLength)+"...."; //取前1000个字符，防止消息太长钉钉无法发送
    }


    /**
     * 根据给定字符生成指定长度字符串
     * @param length
     * @param chars
     * @return
     */
    public static String getLengthString(int length,char chars) {
        char[] data = new char[length];
        for (int i = 0; i < length; i++) {
            data[i] = chars;
        }
        return new String(data);
    }

    public static String listToString(List<Integer> integerList) {
        if (integerList == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean flag = false;
        for (Integer integer : integerList) {
            if (flag) {
                result.append(",");
            } else {
                flag = true;
            }
            result.append(integer);
        }
        return result.toString();
    }


    /**
     * 判断当前位置是否是空格以及连续空格的个数
     * @param s 字符串
     * @param index 位置
     * @return 空格个数
     */
    public static int getBlankNumber(String s, int index)
    {
        if (index < s.length()) {
            if (s.charAt(index) == ' ') {
                return getBlankNumber(s, index + 1) + 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 合并多个空格，将字符串分割为两部分，去掉中间的空格，再连接起来，使连续多个空格变为一个
     * @param s
     * @return
     */
    public static String mergeBlank(String s)
    {
        if (StringUtils.isBlank(s)) return null;
        int numberBlank = 0;
        //字符串的第一部分
        String a1;
        //字符串的第二部分
        String a2;
        //循环整个字符串，判断是否有连续空格
        for (int index = 0; index < s.length(); index++) {
            numberBlank = getBlankNumber(s, index);
            //根据连续空格的个数以及当前的位置，截取字符串
            if (numberBlank >= 2) {
                a1 = s.substring(0, index);
                a2 = s.substring(index + numberBlank - 1, s.length());
                //合并字符串
                s = a1 + a2;
            }
        }
        return s;
    }

    /**
     * 去除字符串首尾空格
     * @param s
     * @return
     */
    public static String trimStr(String s)
    {
        if (StringUtils.isNotBlank(s) && s.charAt(0) == ' ') {
            s = s.substring(1, s.length());
        }
        if (StringUtils.isNotBlank(s) && s.charAt(s.length() - 1) == ' ') {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    /** 首字母大写 */
    public static String toUpperFristChar(String string) {
        char[] charArray = string.toCharArray();
        charArray[0] -= 32;
        return String.valueOf(charArray);
    }

    /** 单引号包裹值 */
    public static String singleQuoteWrap(String string) {
        return "'"+string+"'";
    }

    /** 替换最后匹配字符串 */
    public static String replaceLast(String text, String regex, String replacement) {
        return text.replaceFirst("(?s)"+regex+"(?!.*?"+regex+")", replacement);
    }

    /**
     * 去除空格回车等字符
     * @param str
     * @return
     */
    public  static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
